from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from pyecharts import options as opts
from pyecharts.charts import Line
import re
import time

#浏览器设置
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(executable_path="E:/download/shenxing/BIN/chromedriver.exe",
                          chrome_options=chrome_options)
driver.get("https://yz.chsi.com.cn/zsml/queryAction.do")

#下拉框选择
cs = str(input("请输入城市代码：")) #例如41河南,31上海
Select(driver.find_element_by_name("ssdm")).select_by_value(cs)
xzml = str(input("请输入门类代码：")) #例如zyxw
Select(driver.find_element_by_name("mldm")).select_by_value(xzml)
xzzy = str(input("请输入专业代码：")) #例如0854
Select(driver.find_element_by_name("yjxkdm")).select_by_value(xzzy)
driver.find_element_by_xpath("//*[@id='form2']/table/tbody/tr[4]/td/input").click()
time.sleep(1)

#定义初始界面的句柄
first_window = driver.current_window_handle

def major_and_person_count():
    total_number = 0
    global major_number
    global school_name
    global school_lists
    global total_person
    school_lists = []
    clear_numbers = []
    major_number = []
    total_person = []
    school_list = driver.find_elements_by_xpath("//tbody//tr") #放进循环，每一页school_list都不一样
    for i in school_list:
        clear_numbers = []
        school_name = i.find_element_by_tag_name("a").text
        print(school_name)
        school_lists.append(school_name)
        driver.find_element_by_link_text(school_name).click()
        for current_handle in driver.window_handles:
            if current_handle != first_window:
                driver.switch_to.window(current_handle)
                break
        #爬取研究方向数量
        major = driver.find_elements_by_xpath("//tbody//tr")
        majors = str(len(major))
        major_number.append(majors)
        #爬取拟招生人数
        for i in range(1,len(major)+1):
            number = driver.find_element_by_xpath("//tr[{}]//td[7]//a[1]".format(i)).get_attribute("data-title")
            numbers = re.sub(r"[^0-9]","",number)
            clear_numbers.append(numbers)
            # total_number += int(numbers)
            # print("拟招生人数："+numbers)
        # print(clear_numbers)
        clear_numbers = list(set(clear_numbers))#去除相同的数字
        clear_numbers = list(map(int,clear_numbers))#将str--->int
        total_number = sum(clear_numbers)
        total_person.append(total_number)
        # print(clear_numbers)
        # print(total_number)
        print("研究方向数量:" + majors)
        print("该学校相关研究方向总拟招收人数：",total_number)
        driver.close()
        driver.switch_to.window(first_window)
    print(school_lists)
    print(major_number)
    print(total_person)
    return school_lists,major_number,total_person

def next_page():
    ul = driver.find_element_by_xpath("//ul[@class='ch-page']")
    li = ul.find_elements_by_xpath("li")
    num = 1
    if(len(li) < 10 ):
        pagenumber = int(li[-2].text)
    else:
        pagenumber = int(li[-3].text)
    print("总页数：" + str(pagenumber))
    if(pagenumber == 1):
        major_and_person_count()
    elif(pagenumber < 7):
            while(pagenumber != 0):
                major_and_person_count()
                li[-1].click() #直接点击下一页
                major_and_person_count() #先点击再获取内容
                pagenumber -= 1
    else:
        while(num != pagenumber+1):
            num += 1
            major_and_person_count()
            driver.find_element_by_xpath("//*[@id='goPageNo']").send_keys(num)
            driver.find_element_by_class_name("page-btn").click()

def V_line():
    global major_number
    global school_lists
    global total_person
    x_data = school_lists
    y1_data = major_number
    y2_data = total_person

    (
        Line()
            .add_xaxis(x_data)
            .add_yaxis("研究方向数量", y1_data, is_smooth=True)
            .add_yaxis("拟招生人数", y2_data, is_smooth=True)
            .set_series_opts(
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="研究方向与拟招生人数面积图"),
            xaxis_opts=opts.AxisOpts(
                axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
                is_scale=False,
                boundary_gap=False,
            ),
        )
            .render("line.html")
    )
    print("可视化图表(line.html)绘制成功！")

if __name__ == '__main__':
    next_page()
    # major_and_person_count()
    V_line()
    driver.quit()
